/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunriseset;

import data.enumeration.SRSCity;
import data.sunriseset.Sun;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;
import worker.SunRiseSetWorker;

/**
 *
 * @author bsmith
 */
public class SunRiseSetX {

    private Sun sunXMLObject;

    public SunRiseSetX(SRSCity city, int year, int month, int day) {
        try {
            String sunRiseSetXMLFileName = "/tmp/sunRiseSet.xml";
            File sunRiseSetXMLFile = new File(sunRiseSetXMLFileName);

            if (sunRiseSetXMLFile.exists()) {

                sunRiseSetXMLFile.delete();
            }

            SunRiseSetWorker.getInstance().saveWebServiceFile(sunRiseSetXMLFileName, city, year, month, day);
            sunXMLObject = SunRiseSetWorker.getInstance().buildObject(sunRiseSetXMLFileName);

//            System.out.println(
//                    "SunRiseSet:\n" + sunXMLObject);
        } catch (JAXBException ex) {
            Logger.getLogger(SunRiseSetX.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(SunRiseSetX.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SunRiseSetX.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public void printSunrise(boolean formatted_yes) {
        if (formatted_yes) {
            System.out.print("||Sunrise|");
        }
        System.out.println(sunXMLObject.getMorning().getSunrise());
    }

    public void printSunset(boolean formatted_yes) {
        if (formatted_yes) {
            System.out.print("||Sunset|");
        }
        System.out.println(sunXMLObject.getEvening().getSunset());
    }

    public void printDaylight(boolean formatted_yes) {
        try {
            if (formatted_yes) {
                System.out.print("||Daylight|");
            }
            SimpleDateFormat sdf = new SimpleDateFormat("H:m:s");
            Date sunrise = sdf.parse(sunXMLObject.getMorning().getSunrise());
            Date sunset = sdf.parse(sunXMLObject.getEvening().getSunset());
            long milliseconds = (sunset.getTime() - sunrise.getTime());
            System.out.println(millisecondsToHMSMS(milliseconds));
        } catch (ParseException ex) {
            Logger.getLogger(SunRiseSetX.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private String millisecondsToHMSMS(long milliseconds) {
        long hours = TimeUnit.HOURS.convert(milliseconds, TimeUnit.MILLISECONDS);
        milliseconds = milliseconds - hours * TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS);
        long min = TimeUnit.MINUTES.convert(milliseconds, TimeUnit.MILLISECONDS);
        milliseconds = milliseconds - min * TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES);
        long sec = TimeUnit.SECONDS.convert(milliseconds, TimeUnit.MILLISECONDS);
        milliseconds = milliseconds - sec * TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS);
        return (hours+"h "+min+"m "+sec+"s "+milliseconds+"ms");
    }
}
